rm(list = ls())

library(data.table)
library(forcats)
library(dplyr)
library(tidyr)
library(ggplot2)

data_pooled <- readRDS('./replication_hasz/output/data/data_pooled.rds')
data <- data_pooled[study!='Pooled']

plot_data <- data.table(gather(data[study=='Florida Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
plot_data <- plot_data[, .N, by=.(treatment, outcome, value)]
plot_data <- plot_data[complete.cases(plot_data)]
plot_data[, sum_N:=sum(N), by=.(treatment, outcome)]
plot_data[, prop:=N/sum_N]
plot_data[treatment==1, treatmentl:='control']
plot_data[treatment==2, treatmentl:='co-partisan']
plot_data[treatment==3, treatmentl:='counter-partisan']
plot_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
plot_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
plot_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
plot_data[outcome=='more_infoc', outcomel:='Willingness to learn']
plot_data$treatmentl <- factor(plot_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))

mean_data <- data.table(gather(data[study=='Florida Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
mean_data <- mean_data[, .(mean=mean(value, na.rm=T), se=sd(value, na.rm = T)/sqrt(.N)), by=.(outcome, treatment)]
mean_data[treatment==1, treatmentl:='control']
mean_data[treatment==2, treatmentl:='co-partisan']
mean_data[treatment==3, treatmentl:='counter-partisan']
mean_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
mean_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
mean_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
mean_data[outcome=='more_infoc', outcomel:='Willingness to learn']
mean_data$treatmentl <- factor(mean_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))

ggplot() + 
  aes(group=treatment, color=treatmentl, fill=treatmentl) + 
  geom_rect(data=mean_data, aes(ymin=-Inf, ymax=Inf, xmin=mean-1.96*se, xmax=mean+1.96*se), alpha=0.4, color=NA) + 
  geom_line(data = plot_data, aes(x=value, y=prop)) + 
  geom_vline(data=mean_data, aes(xintercept=mean, color=treatmentl)) + 
  facet_wrap(~forcats::fct_rev(outcomel), scales='free') +
  scale_y_continuous(breaks=seq(0, 0.8, 0.2)) +
  xlab('Support for noncitizen voting') + ylab('Proportion') +
  scale_fill_grey(start = 0.2, end = 0.8, name='Experimental condition') +
  scale_color_grey(start = 0.2, end = 0.8, name='Experimental condition') + 
  theme_bw(base_size =12) +
  theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD1a.pdf', width=8, height=6)


plot_data <- data.table(gather(data[study=='U.S. Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
plot_data <- plot_data[, .N, by=.(treatment, outcome, value)]
plot_data <- plot_data[complete.cases(plot_data)]
plot_data[, sum_N:=sum(N), by=.(treatment, outcome)]
plot_data[, prop:=N/sum_N]
plot_data[treatment==1, treatmentl:='control']
plot_data[treatment==2, treatmentl:='co-partisan']
plot_data[treatment==3, treatmentl:='counter-partisan']
plot_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
plot_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
plot_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
plot_data[outcome=='more_infoc', outcomel:='Willingness to learn']
plot_data$treatmentl <- factor(plot_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))


mean_data <- data.table(gather(data[study=='U.S. Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
mean_data <- mean_data[, .(mean=mean(value, na.rm=T), se=sd(value, na.rm = T)/sqrt(.N)), by=.(outcome, treatment)]
mean_data[treatment==1, treatmentl:='control']
mean_data[treatment==2, treatmentl:='co-partisan']
mean_data[treatment==3, treatmentl:='counter-partisan']
mean_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
mean_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
mean_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
mean_data[outcome=='more_infoc', outcomel:='Willingness to learn']
mean_data$treatmentl <- factor(mean_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))

ggplot() + 
  aes(group=treatment, color=treatmentl, fill=treatmentl) + 
  geom_rect(data=mean_data, aes(ymin=-Inf, ymax=Inf, xmin=mean-1.96*se, xmax=mean+1.96*se), alpha=0.4, color=NA) + 
  geom_line(data = plot_data, aes(x=value, y=prop)) +
  geom_vline(data=mean_data, aes(xintercept=mean, color=treatmentl)) +
  xlab('Support for noncitizen voting') + ylab('Proportion') +
  scale_fill_grey(start = 0.2, end = 0.8, name='Experimental condition') +
  scale_color_grey(start = 0.2, end = 0.8, name='Experimental condition') + 
  facet_wrap(~forcats::fct_rev(outcomel), scales='free') +
  scale_y_continuous(breaks=seq(0, .8, 0.2)) +
  theme_bw(base_size = 12) +
  theme(legend.position = "bottom") 
ggsave('./replication_hasz/output/figures/figD1b.pdf', width=8, height=6)


#### By party in Study 2 ####
# Dems
plot_data <- data.table(gather(data[democrat==1 & study=='U.S. Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
plot_data <- plot_data[, .N, by=.(treatment, outcome, value)]
plot_data <- plot_data[complete.cases(plot_data)]
plot_data[, sum_N:=sum(N), by=.(treatment, outcome)]
plot_data[, prop:=N/sum_N]
plot_data[treatment==1, treatmentl:='control']
plot_data[treatment==2, treatmentl:='co-partisan']
plot_data[treatment==3, treatmentl:='counter-partisan']
plot_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
plot_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
plot_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
plot_data[outcome=='more_infoc', outcomel:='Willingness to learn']
plot_data$treatmentl <- factor(plot_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))


mean_data <- data.table(gather(data[democrat==1 & study=='U.S. Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
mean_data <- mean_data[, .(mean=mean(value, na.rm=T), se=sd(value, na.rm = T)/sqrt(.N)), by=.(outcome, treatment)]
mean_data[treatment==1, treatmentl:='control']
mean_data[treatment==2, treatmentl:='co-partisan']
mean_data[treatment==3, treatmentl:='counter-partisan']
mean_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
mean_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
mean_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
mean_data[outcome=='more_infoc', outcomel:='Willingness to learn']
mean_data$treatmentl <- factor(mean_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))


ggplot() + 
  aes(group=treatment, color=treatmentl, fill=treatmentl) + 
  geom_rect(data=mean_data, aes(ymin=-Inf, ymax=Inf, xmin=mean-1.96*se, xmax=mean+1.96*se), alpha=0.4, color=NA) + 
  geom_line(data = plot_data, aes(x=value, y=prop)) + 
  geom_vline(data=mean_data, aes(xintercept=mean, color=treatmentl)) + 
  facet_wrap(~forcats::fct_rev(outcomel), scales='free') +
  xlab('Support for noncitizen voting') + ylab('Proportion') +
  scale_fill_grey(start = 0.2, end = 0.8, name='Experimental condition') +
  scale_color_grey(start = 0.2, end = 0.8, name='Experimental condition') +
  #ylim(0,.8) +
  theme_bw(base_size = 12) +
  theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD3a.pdf', width=8, height=6)

# Reps
plot_data <- data.table(gather(data[republican==1 & study=='U.S. Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
plot_data <- plot_data[, .N, by=.(treatment, outcome, value)]
plot_data <- plot_data[complete.cases(plot_data)]
plot_data[, sum_N:=sum(N), by=.(treatment, outcome)]
plot_data[, prop:=N/sum_N]
plot_data[treatment==1, treatmentl:='control']
plot_data[treatment==2, treatmentl:='co-partisan']
plot_data[treatment==3, treatmentl:='counter-partisan']
plot_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
plot_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
plot_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
plot_data[outcome=='more_infoc', outcomel:='Willingness to learn']
plot_data$treatmentl <- factor(plot_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))


mean_data <- data.table(gather(data[republican==1 & study=='U.S. Study',.(treatment, vote_usc, vote_ownc, ballotc, more_infoc )], "outcome", "value", -treatment))
mean_data <- mean_data[, .(mean=mean(value, na.rm=T), se=sd(value, na.rm = T)/sqrt(.N)), by=.(outcome, treatment)]
mean_data[treatment==1, treatmentl:='control']
mean_data[treatment==2, treatmentl:='co-partisan']
mean_data[treatment==3, treatmentl:='counter-partisan']
mean_data[outcome=='vote_usc', outcomel:='Vote U.S. local elections']
mean_data[outcome=='vote_ownc', outcomel:='Vote own local elections']
mean_data[outcome=='ballotc', outcomel:='Vote STATE local elections']
mean_data[outcome=='more_infoc', outcomel:='Willingness to learn']
mean_data$treatmentl <- factor(mean_data$treatmentl, levels=c('control','co-partisan','counter-partisan'))

ggplot() + 
  aes(group=treatment, color=treatmentl, fill=treatmentl) + 
  geom_rect(data=mean_data, aes(ymin=-Inf, ymax=Inf, xmin=mean-1.96*se, xmax=mean+1.96*se), alpha=0.4, color=NA) + 
  geom_line(data = plot_data, aes(x=value, y=prop)) + 
  geom_vline(data=mean_data, aes(xintercept=mean, color=treatmentl)) + 
  facet_wrap(~forcats::fct_rev(outcomel), scales='free') +
  #ylim(0,.8) +
  xlab('Support for noncitizen voting') + ylab('Proportion') +
  scale_fill_grey(start = 0.2, end = 0.8, name='Experimental condition') +
  scale_color_grey(start = 0.2, end = 0.8, name='Experimental condition') + 
  theme_bw() +
  theme(legend.position = 'bottom')
ggsave('./replication_hasz/output/figures/figD3b.pdf', width=8, height=6)
